﻿using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Utilities;
using Process.Entities;
using System.Collections.Generic;

namespace Process.DAL
{
    public class BrokerDAL
    {
        private static BrokerENT CreateBrokerFromReader(IDataReader reader)
        {
            BrokerENT item = new BrokerENT();

            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BrokerID.ToString()))) item.BrokerID = Utility.StringParse(reader[BrokerColumns.BrokerID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BusinessUsername.ToString()))) item.BusinessUsername = Utility.StringParse(reader[BrokerColumns.BusinessUsername.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BusinessPassword.ToString()))) item.BusinessPassword = Utility.StringParse(reader[BrokerColumns.BusinessPassword.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BrokerName.ToString()))) item.BrokerName = Utility.StringParse(reader[BrokerColumns.BrokerName.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BrokerAddress.ToString()))) item.BrokerAddress = Utility.StringParse(reader[BrokerColumns.BrokerAddress.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BrokerPhone.ToString()))) item.BrokerPhone = Utility.StringParse(reader[BrokerColumns.BrokerPhone.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BrokerContactPerson.ToString()))) item.BrokerContactPerson = Utility.StringParse(reader[BrokerColumns.BrokerContactPerson.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BrokerPaymentCard.ToString()))) item.BrokerPaymentCard = Utility.StringParse(reader[BrokerColumns.BrokerPaymentCard.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.Amount.ToString()))) item.Amount = Utility.DecimalParse(reader[BrokerColumns.Amount.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.BeginDate.ToString()))) item.BeginDate = Utility.DateTimeParse(reader[BrokerColumns.BeginDate.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.EndDate.ToString()))) item.EndDate = Utility.DateTimeParse(reader[BrokerColumns.EndDate.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.WS1.ToString()))) item.WS1 = Utility.StringParse(reader[BrokerColumns.WS1.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.WS2.ToString()))) item.WS2 = Utility.StringParse(reader[BrokerColumns.WS2.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.WS3.ToString()))) item.WS3 = Utility.StringParse(reader[BrokerColumns.WS3.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(BrokerColumns.IsDeleted.ToString()))) item.IsDeleted = Utility.BoolParse(reader[BrokerColumns.IsDeleted.ToString()]);

            return item;
        }

        public static BrokerENT GetBrokerByID(string brokerID)
        {
            try
            {
                BrokerENT broker = new BrokerENT();

                DataAccessHelper.AddParameter("BrokerID", SqlDbType.VarChar, brokerID);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procBrokerGetByID"))
                {
                    while (reader.Read())
                    {
                        broker = CreateBrokerFromReader(reader);
                    }
                }

                return broker;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà môi giới.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public static List<BrokerENT> GetBrokerList()
        {
            try
            {
                List<BrokerENT> brokerList = new List<BrokerENT>();

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procBrokerGetList"))
                {
                    while (reader.Read())
                    {
                        brokerList.Add(CreateBrokerFromReader(reader));
                    }
                }

                return brokerList;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà môi giới.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}
